package com.umeng.community.example.Utils;

import android.content.Context;
import android.content.Intent;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.qianxx.base.IConstants;
import com.qianxx.base.Urls;
import com.qianxx.base.request.Config;
import com.qianxx.base.request.RM;
import com.qianxx.base.request.RequestBean;
import com.qianxx.base.request.RequestCallback;
import com.qianxx.base.request.RequestUtil;
import com.qianxx.base.utils.SPUtil;
import com.qianxx.base.utils.ToastUtil;
import com.umeng.comm.core.beans.CommUser;
import com.umeng.comm.core.beans.Source;
import com.umeng.comm.ui.adapters.FeedLikeAdapter;
import com.umeng.comm.ui.imagepicker.util.BroadcastUtils;
import com.umeng.community.example.MainActivity;
import com.umeng.community.example.bean.MyBean;
import com.umeng.community.example.bean.MyInfo;
import com.umeng.community.example.bean.UserBean;

import java.util.HashMap;

/**
 * Created by zhangbingyong on 16/5/19.
 */
public class OtherUserRequestUtils implements RequestCallback {
    private OtherUserRequestUtils() {
    }

    private static OtherUserRequestUtils requestUtils;
    private static Context mContext;
    protected RequestQueue mRequest; // 请求队列对象
    private MyInfo info;

    public static OtherUserRequestUtils getInstance(Context context) {

        if (requestUtils == null) {
            synchronized (OtherUserRequestUtils.class) {
                if (requestUtils == null) {
                    requestUtils = new OtherUserRequestUtils();

                }
            }
        }
        mContext = context;

        return requestUtils;
    }

    /**
     * 获取其他用户的信息
     */
    public void OtherUser(CommUser userInfo, String source, GetOtherUserInfoListener getOtherUserInfoListener) {
        this.getOtherUserInfoListener = getOtherUserInfoListener;
        HashMap<String, String> map = new HashMap<>();
        if (source.equals("qq")) {
            map.put("type", "qq");
        } else if (source.equals("wxsession")) {
            map.put("type", "wx");
        } else if (source.equals("sina")) {
            map.put("type", "wb");
        }
        map.put("openId", userInfo.sourceUid);
        requestDataWithoutLoading(IConstants.REQUEST, Urls.OTHERUSER_GET, RM.POST, MyBean.class, map);
    }

    @Override
    public void requestSuccess(RequestBean baseBean, Config config) {
        MyBean bean = (MyBean) baseBean;
        info = bean.getData();
        getOtherUserInfoListener.setOtherUserInfo(info);
    }


    /**
     * 不带loading指示的数据请求
     */
    private void requestDataWithoutLoading(String requestTag, String url,
                                           RM method, Class<? extends RequestBean> bean,
                                           HashMap<String, String> params) {
        if (mRequest == null) {
            mRequest = Volley.newRequestQueue(mContext);
        }
        RequestUtil.requestData(mRequest, requestTag, url, method, bean,
                params, this, new Config().setShowLoading(false));
    }


    @Override
    public void requestFail(RequestBean bean, Config config) {

    }

    @Override
    public void requestTokenInvalid(String requestTag, Config config) {

    }

    private GetOtherUserInfoListener getOtherUserInfoListener;

    public GetOtherUserInfoListener getGetUserInfoListener() {
        return getOtherUserInfoListener;
    }

    public void setGetOtherUserInfoListener(GetOtherUserInfoListener getOtherUserInfoListener) {
        this.getOtherUserInfoListener = getOtherUserInfoListener;
    }

    public interface GetOtherUserInfoListener{
        public void setOtherUserInfo(MyInfo myInfo);
    }
}
